<?php

namespace app\controller\admin;
use app\common\service\FileService;
use think\annotation\route\Group;
use think\annotation\route\Route;
use think\annotation\route\Post;

#[Group('admin/file')]
class FileController extends AdminAuthorizedController
{
    #[Post('image')]
    public function image()
    {
        /*$pipe = input('pipe', 'NOP');
        ['path' => $path] = $this->FileService()->uploadImage('admin');
        // 管道兼容处理
        if ($pipe === 'WANGEDITOR') {
            // 老王富文本
            $alt = input('WANGEDITOR-alt');
            $href = input('WANGEDITOR-href');
            return json([
                'errno' => 0,
                'data' => [
                    'url' => $path,
                    'alt' => $alt,
                    'href' => $href
                ]
            ]);
        } else {
            return ['path' => $path];
        }*/
        return $this->FileService()->uploadImage('admin');
    }

    #[Post('images')]
    public function images()
    {
        return $this->FileService()->uploadImages('admin');
    }

    #[Post('video')]
    public function video()
    {
        /*$pipe = input('pipe', 'NOP');
        ['path' => $path] = $this->FileService()->uploadVideo('admin');
        // 管道兼容处理
        if ($pipe === 'WANGEDITOR') {
            // 老王富文本
            $poster = input('WANGEDITOR-poster');
            return json([
                'errno' => 0,
                'data' => [
                    'url' => $path,
                    'poster' => $poster
                ]
            ]);
        } else {
            return ['path' => $path];
        }*/
        return $this->FileService()->uploadVideo('admin');
    }

    #[Post('attachment')]
    public function attachment()
    {
        /*$pipe = input('pipe', 'NOP');
        ['path' => $path] = $this->FileService()->uploadAttachment('admin');
        // 管道兼容处理
        if ($pipe === 'WANGEDITOR') {
            // 老王富文本
            $poster = input('WANGEDITOR-poster');
            return json([
                'errno' => 0,
                'data' => [
                    'url' => $path,
                    'poster' => $poster
                ]
            ]);
        } else {
            return ['path' => $path];
        }*/
        return $this->FileService()->uploadAttachment('admin');
    }

    #[Route('GET|POST', 'excel')]
    public function excel(){

        $param = request()->param();

        $rule = [
            "path" => "require",
            "start" => "require",
            "end" => "require"
        ];

        $this->validate($param,$rule);

        return $this->FileService()->importExcel($param);
    }

    protected function FileService()
    {
        return new FileService($this->app);
    }
}
